﻿using System;
using System.Collections.Generic;
using Common;
using System.Web.Script.Serialization;
using Educational.Business;
using Basic.Business;
using Educational.VO;

public partial class Educational_Ajax_TeacherManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //强制刷新页面
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        Response.AppendHeader("Pragma", "No-Cache"); 

        JavaScriptSerializer json = new JavaScriptSerializer();
        if (!String.IsNullOrEmpty(Request.QueryString["Action"]))
        {
            string action = BadChar.ReplaceBadChar(Request.QueryString["Action"]);
            switch (action)
            {
                #region deleteTeacher删除教师
                case "deleteTeacher":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            string teacherIds = Request.QueryString["teacherIds"];
                            int[] teacherId = Tools.DeleteIntArrayRepeat(Tools.StringArrayToIntArray(teacherIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)));
                            TeacherAdmin.DeleteTeacher(school.schoolId, teacherId);
                            result.error = false;
                            result.msg = "删除教师成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region saveUpdateTeacher保存教师修改
                case "saveUpdateTeacher":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            int teacherId = int.Parse(Request.Form["teacherId"]);
                            string loginPass = BadChar.ReplaceBadChar(Request.Form["loginPass"].Trim());
                            string confirmLoginPass = BadChar.ReplaceBadChar(Request.Form["confirmLoginPass"].Trim());
                            int jobTitle = int.Parse(Request.Form["jobTitle2"]);
                            int status = int.Parse(Request.Form["status"]);
                            if (status == 8)
                                throw new Exception("你在执行一个你没有权限的操作。经过系统判断，你应该在试探攻击本系统，送你两个字：SB");
                            if (!loginPass.Equals(confirmLoginPass))
                                throw new Exception("两次密码不一致。。");
                            if (String.IsNullOrEmpty(loginPass))
                                TeacherAdmin.UpdateTeacher(teacherId, null, status, jobTitle, school.schoolId);
                            else
                                TeacherAdmin.UpdateTeacher(teacherId, Tools.MD5Encrypt(loginPass), status, jobTitle, school.schoolId);
                            result.error = false;
                            result.msg = "修改教师成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region saveAddTeacher保存教师添加
                case "saveAddTeacher":
                    {
                        Result<Object> result = new Result<Object>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            School school = (School)Session["school"];
                            string teacherName = BadChar.ReplaceBadChar(Request.Form["teacherName"].Trim());
                            string loginId = BadChar.ReplaceBadChar(Request.Form["loginId"].Trim());
                            string loginPass = BadChar.ReplaceBadChar(Request.Form["loginPass"].Trim());
                            string confirmLoginPass = BadChar.ReplaceBadChar(Request.Form["confirmLoginPass"].Trim());
                            int jobTitle = int.Parse(Request.Form["jobTitle"]);
                            if (!loginPass.Equals(confirmLoginPass))
                                throw new Exception("两次密码不一致。。");
                            TeacherAdmin.AddTeacher(teacherName, loginId, Tools.MD5Encrypt(loginPass), jobTitle, school.schoolId);
                            result.error = false;
                            result.msg = "添加教师成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region search查询列表
                case "search":
                    {
                        Result<Teacher> result = new Result<Teacher>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            string teacherName = String.Empty, loginId = String.Empty;
                            if (!String.IsNullOrEmpty(Request.QueryString["teacherName"]))
                                teacherName = BadChar.ReplaceBadChar(Request.QueryString["teacherName"].Trim());
                            if (!String.IsNullOrEmpty(Request.QueryString["loginId"]))
                                loginId = BadChar.ReplaceBadChar(Request.QueryString["loginId"].Trim());
                            int page = int.Parse(Request["page"]);
                            int perPage = int.Parse(Request["perpage"]);
                            School school = (School)Session["school"];
                            PageResult<Teacher> pageResult = TeacherAdmin.GetsTeacher(teacherName, loginId, school.schoolId, (page - 1) * perPage, page * perPage);
                            result.data = pageResult.data;
                            result.total = pageResult.totalCount;
                            result.page = page;
                            result.error = false;
                            result.msg = "查询教师列表成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                #region searchForSelectTeacher查询状态正常教师列表
                case "searchForSelectTeacher":
                    {
                        Result<Teacher> result = new Result<Teacher>();
                        try
                        {
                            if (!SchoolAdmin.IsSchoolSessionValid())
                                throw new Exception("您的登录已超时，请重新登录！");
                            string teacherName = String.Empty, loginId = String.Empty;
                            if (!String.IsNullOrEmpty(Request.QueryString["teacherName"]))
                                teacherName = BadChar.ReplaceBadChar(Request.QueryString["teacherName"].Trim());
                            if (!String.IsNullOrEmpty(Request.QueryString["loginId"]))
                                loginId = BadChar.ReplaceBadChar(Request.QueryString["loginId"].Trim());
                            int page = int.Parse(Request["page"]);
                            int perPage = int.Parse(Request["perpage"]);
                            School school = (School)Session["school"];
                            PageResult<Teacher> pageResult = TeacherAdmin.GetsTeacherForSelect(teacherName, loginId, school.schoolId, (page - 1) * perPage, page * perPage);
                            result.data = pageResult.data;
                            result.total = pageResult.totalCount;
                            result.page = page;
                            result.error = false;
                            result.msg = "查询教师列表成功";
                        }
                        catch (Exception ex)
                        {
                            result.error = true;
                            result.msg = ex.Message;
                        }
                        Response.Write(json.Serialize(result));
                    }
                    break;
                #endregion
                default:
                    {
                        Result<object> result = new Result<object>();
                        result.error = true;
                        result.msg = StringTable.ErrorUnknowAction;
                        Response.Write(json.Serialize(result));
                    }
                    break;
            }
        }
        else
        {
            Result<object> result = new Result<object>();
            result.error = true;
            result.msg = StringTable.ErrorMissingParameters;
            Response.Write(json.Serialize(result));
        }
        Response.End();
    }
}
